<template>
  <el-dialog title="编辑用户" width="60%" :before-close="handleClose">
    <el-form :model="userForm" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input v-model="userForm.username" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电子邮箱">
            <el-input
              v-model="userForm.email"
              placeholder="请输入电子邮箱"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input
              v-model="userForm.fullName"
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码">
            <el-input
              v-model="userForm.phone"
              placeholder="请输入手机号码"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="角色">
            <el-select
              v-model="userForm.role"
              placeholder="请选择角色"
              style="width: 100%"
            >
              <el-option label="系统管理员" value="admin"></el-option>
              <el-option label="培训导师" value="trainer"></el-option>
              <el-option label="普通用户" value="user"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-select
              v-model="userForm.department"
              placeholder="请选择部门"
              style="width: 100%"
            >
              <el-option label="护理部" value="nursing"></el-option>
              <el-option label="内科" value="internal"></el-option>
              <el-option label="外科" value="surgery"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="职位">
            <el-input
              v-model="userForm.position"
              placeholder="请输入职位"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号状态">
            <el-switch
              v-model="userForm.status"
              active-text="启用"
              inactive-text="禁用"
              active-value="active"
              inactive-value="inactive"
            ></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="重置密码">
        <el-button type="warning" @click="resetPassword">重置密码</el-button>
        <span class="ms-3 text-muted">重置后密码将发送至用户邮箱</span>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveUser">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from "vue";
import { modalControllers } from "@/stores/modalController.js";

const modalController = modalControllers();

const userForm = reactive({
  username: "",
  email: "",
  fullName: "",
  phone: "",
  role: "",
  department: "",
  position: "",
  status: "active",
});

const handleClose = (done) => {
  // You can add a confirmation dialog here if needed
  done();
};

const resetPassword = () => {
  console.log("Reset password for user:", userForm.username);
  // Implement password reset logic here
};

const saveUser = () => {
  console.log("User data:", userForm);
  // Implement user update logic here
  dialogVisible.value = false;
};

const open = (userData) => {
  Object.assign(userForm, userData);
  dialogVisible.value = true;
};

defineExpose({
  open,
});
</script>

<style scoped>
/* Add any specific styles for the modal here */
</style>
